import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { environment } from '@env/environment';
import { MessageService } from '../../message/service/message.service';
import { NzNotificationService } from 'ng-zorro-antd/notification';
import { NzDrawerService } from 'ng-zorro-antd/drawer';
import { SystemDictService } from '../../../system/dict/service/service.service';
import * as moment from 'moment';
import { ProblemService } from '../service/problem.service';
import { NzModalService } from 'ng-zorro-antd/modal';
import { SystemDictListEditComponent } from '../../../system/dict/list-edit/list-edit.component';
import { ProblemEditComponent } from '../edit/edit.component';

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less'],
})
export class ProblemListComponent implements OnInit {
  searchForm: any = FormGroup;
  dataList: any;
  statList: any;
  pageInfo = {
    pi: 1,
    ps: 10,
    total: 0,
    loading: false,
  };
  yesOrNo = environment.yesNo;

  constructor(
    private fb: FormBuilder,
    private service: ProblemService,
    private notification: NzNotificationService,
    private modalService: NzModalService,
    private dictService: SystemDictService,
  ) {}

  ngOnInit(): void {
    this.searchForm = this.fb.group({
      theme: null,
      userName: null,
      createTime: null,
    });
    this.dictService.dictList(environment.dict.stationStat).subscribe((res) => {
      this.statList = res.data;
    });
    this.load();
  }

  add(): void {
    this.modalService
      .create({
        nzContent: ProblemEditComponent,
        nzComponentParams: {},
        nzTitle: '添加常见问题',
        nzFooter: null,
      })
      .afterClose.subscribe((res: any) => {
        if (res) {
          this.load();
        }
      });
  }

  edit(record: any): void {
    this.modalService
      .create({
        nzContent: ProblemEditComponent,
        nzComponentParams: { record },
        nzTitle: '编辑常见问题',
        nzFooter: null,
      })
      .afterClose.subscribe((res: any) => {
        if (res) {
          this.load();
        }
      });
  }

  reset(): void {
    this.pageInfo.pi = 1;
    this.searchForm.reset();
    this.load();
  }

  search(): void {
    this.pageInfo.pi = 1;
    this.load();
  }

  load(): void {
    const params = {
      ...this.searchForm.value,
      current: this.pageInfo.pi,
      size: this.pageInfo.ps,
    };

    if (this.searchForm.value.createTime !== null && this.searchForm.value.createTime.length > 0) {
      params['createTimeStart'] = moment(this.searchForm.value.createTime[0]).format('YYYY-MM-DD');
      params['createTimeEnd'] = moment(this.searchForm.value.createTime[1]).format('YYYY-MM-DD');
      delete params['createTime'];
    }
    //删除没有的属性和空的
    for (let key in params) {
      if (params.hasOwnProperty(key) && (params[key] == null || params[key] == undefined || params[key] === ' ')) {
        delete params[key];
      }
    }
    this.pageInfo.loading = true;
    this.service.page(params).subscribe((res) => {
      this.dataList = res.data.records;
      this.pageInfo.total = res.data.total;
      this.pageInfo.loading = false;
    });
  }

  remove(id: string): void {
    this.service.remove(id).subscribe((res) => {
      this.load();
    });
  }
}
